/**
 * 
 */
package cn.com.xf.service.impl;

import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.xf.common.ConfigUtil;
import cn.com.xf.common.Constants;
import cn.com.xf.dao.SmsSendDAO;
import cn.com.xf.domain.SmsSend;
import cn.com.xf.service.SmsService;
import cn.com.xf.web.form.NoticeForm;

/**
 * @author Fangjinsong
 * 
 */
@Service
public class SmsServiceImpl implements SmsService {
	@Autowired
	private SmsSendDAO smsSendDAO;

	@Override
	public void insertSmsList(NoticeForm form) throws Exception {
		// 1.获取标题内容
		String title = form.getTitle();
		String content = form.getContent();
		// 只析取文字信息
		Document jsoup = Jsoup.parse(content);
		content = jsoup.text();
		byte bytes[] = { (byte) 0xC2, (byte) 0xA0 };
		String UTFSpace = new String(bytes, "utf-8");
		content = content.replaceAll(UTFSpace, "");
		content = content.replaceAll(" ", "");
		int sms_length = Integer.valueOf(ConfigUtil.getValue(Constants.CONFIG_SMS_LENGTH));
		if (content.length() > sms_length) {
			content = content.substring(0, sms_length - 1);
		}
		// 2.获取手机号码
		String mobiles = form.getMobiles();
		if (StringUtils.isEmpty(mobiles))
			return;
		if (mobiles.split(",").length == 0)
			return;
		ArrayList<SmsSend> list = new ArrayList<SmsSend>();
		for (String s : mobiles.split(",")) {
			SmsSend smsSend = new SmsSend();
			smsSend.setMsgContent(content);
			smsSend.setMsgMobile(s);
			smsSend.setMsgStatus(Short.valueOf("0"));
			smsSend.setMsgType("7");
			smsSend.setMsgEntertimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			list.add(smsSend);
		}
		// 写入sms_send表
		smsSendDAO.insertBatch(list);
	}
}
